//
//  ZPZhuanClassViewController.m
//  ZPIOSEducation
//
//  Created by mini1 on 2017/1/13.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "ZPZhuanClassViewController.h"
#import "ZPUpdateCurriculumCell.h"
#import "MJRefresh.h"
#import "ZPQuitClassCell.h"
static NSString * const reuseIdentifier_update = @"ZPUpdateCurriculumCell";


@interface ZPZhuanClassViewController () <UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *zhuanClassTableView;
@property (weak, nonatomic) IBOutlet UIView *emptyView;
@property (strong, nonatomic) ZPUpdateCurriculumCell *updateCell;
@property (strong, nonatomic) NSMutableArray *dataArr;
@property (assign,nonatomic) NSInteger pagenumber;
@property (assign,nonatomic) BOOL hasNextPage;
@end

@implementation ZPZhuanClassViewController
- (ZPUpdateCurriculumCell *)updateCell
{
    if (_updateCell == nil) {
        _updateCell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ZPUpdateCurriculumCell class]) owner:self options:nil] lastObject];
    }
    return _updateCell;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.zhuanClassTableView.delegate = self;
    self.zhuanClassTableView.dataSource = self;
    hud = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:hud];
    _dataArr = [NSMutableArray array];
    [_zhuanClassTableView registerNib:[UINib nibWithNibName:@"ZPUpdateCurriculumCell" bundle:nil] forCellReuseIdentifier:@"ZPUpdateCurriculumCell"];
    [_zhuanClassTableView registerNib:[UINib nibWithNibName:@"ZPQuitClassCell" bundle:nil] forCellReuseIdentifier:@"ZPQuitClassCell"];
    
    
    
    _pagenumber = 1;
    __weak typeof(self) weakSelf = self;
    [_zhuanClassTableView addLegendFooterWithRefreshingBlock:^{
        if (weakSelf.hasNextPage) {
            weakSelf.pagenumber++;
            [weakSelf getChangeRecord];
        }else{
            if (_zhuanClassTableView.footer.isRefreshing) {
                [_zhuanClassTableView.footer endRefreshing];
            }
        }
    }];
    
    [self getChangeRecord];
}



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
}






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *tm= [_dataArr objectAtIndex:indexPath.row];
    if ([[tm objectForKey:@"type"] integerValue]==0) {
        ZPQuitClassCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZPQuitClassCell"];
        cell.eventDate.text=[tm objectForKey:@"createTimeString"];
        cell.courseDate.text = [tm objectForKey:@"oldDateString"];
        if ([[tm objectForKey:@"status"] integerValue]==0) {
            cell.eventState.text=@"待审核";
        }else if ([[tm objectForKey:@"status"] integerValue]==1) {
            cell.eventState.text=@"已取消";
        }else if ([[tm objectForKey:@"status"] integerValue]==2){
            cell.eventState.text=@"待退款";
        }else if ([[tm objectForKey:@"status"] integerValue]==3){
            cell.eventState.text=@"退款中";
        }else if ([[tm objectForKey:@"status"] integerValue]==4){
            cell.eventState.text=@"已退款";
        }else{
            cell.eventState.text=@"退款失败";
        }
        cell.courseName.text=[tm objectForKey:@"oldName"];
        cell.refundAmount.text=[NSString stringWithFormat:@"退款金额：¥%@",[tm objectForKey:@"totalSum"]];
        cell.courseAddress.text=[tm objectForKey:@"oldTeachName"];
        cell.courseTeacher.text=[tm objectForKey:@"oldTeacherName"];
        cell.course = [tm objectForKey:@"course"];
        return cell;
    }else{
        ZPUpdateCurriculumCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZPUpdateCurriculumCell"];
        if ([[tm objectForKey:@"type"] integerValue]==1) {
            cell.eventType.text = @"转班";
            cell.eventType.backgroundColor = zMainColor;
            cell.eventTypeImgV.image=Z_IMAGE(@"zhenpin_zhuanban");
            if ([[tm objectForKey:@"status"] integerValue]==0) {
                cell.eventState.text = @"待审核";
                cell.eventState.textColor=UIColorFromRGB(0xff5656);
            }else{
                cell.eventState.text = @"已转班";
                cell.eventState.textColor=UIColorFromRGB(0x21a4eb);
            }
        }else{
            cell.eventType.text = @"调课";
            cell.eventType.backgroundColor = UIColorFromRGB(0xF5A623);
            cell.eventTypeImgV.image=Z_IMAGE(@"zhenpin_tiaoke");
            if ([[tm objectForKey:@"status"] integerValue]==0) {
                cell.eventState.text = @"待审核";
                cell.eventState.textColor=UIColorFromRGB(0xff5656);
            }else{
                cell.eventState.text = @"已调课";
                cell.eventState.textColor=UIColorFromRGB(0x21a4eb);
            }
        }
        cell.eventDate.text = toStr([tm objectForKey:@"createTimeString"]);
        
        cell.courseName.text=[tm objectForKey:@"oldName"];
        cell.courseDate.text=[tm objectForKey:@"oldDateString"];
        cell.courseTeacher.text=[tm objectForKey:@"oldTeacherName"];
        cell.courseAddress.text=[tm objectForKey:@"oldTeachName"];
        cell.to_courseName.text=[tm objectForKey:@"newName"];
        cell.to_courseDate.text=[tm objectForKey:@"newDateString"];
        cell.to_courseAddress.text=[tm objectForKey:@"newTeachName"];
        cell.to_courseTeacher.text=[tm objectForKey:@"newTeacherName"];
        cell.course = [tm objectForKey:@"course"];
        return cell;
    }
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *tm= [_dataArr objectAtIndex:indexPath.row];
    if ([[tm objectForKey:@"type"] integerValue]==0) {
        
        NSArray *arr=[tm objectForKey:@"course"];
        
        int v= arr.count/10.0;
        
        float h1 = ceilf([toStr([tm objectForKey:@"oldName"]) calculateSize:CGSizeMake(SCREENWIDTH-26, 9999) font:[UIFont systemFontOfSize:14]].height);
        float h2 = ceilf([toStr([tm objectForKey:@"oldTeachName"]) calculateSize:CGSizeMake(SCREENWIDTH-93, 9999) font:[UIFont systemFontOfSize:12]].height);
        return 161+23*v+h1+h2;
    }else{
        NSArray *arr=[tm objectForKey:@"course"];
        float h1 = ceilf([toStr([tm objectForKey:@"oldName"]) calculateSize:CGSizeMake(SCREENWIDTH-26, 9999) font:[UIFont systemFontOfSize:14]].height);
        float h2 = ceilf([toStr([tm objectForKey:@"oldTeachName"]) calculateSize:CGSizeMake(SCREENWIDTH-90, 9999) font:[UIFont systemFontOfSize:12]].height);
        float h3 = ceilf([toStr([tm objectForKey:@"newName"]) calculateSize:CGSizeMake(SCREENWIDTH-26, 9999) font:[UIFont systemFontOfSize:14]].height);
        float h4 = ceilf([toStr([tm objectForKey:@"newTeachName"]) calculateSize:CGSizeMake(SCREENWIDTH-90, 9999) font:[UIFont systemFontOfSize:12]].height);
        int v= arr.count/10.0;
        return 258+23*v+h1+h2+h3+h4;
    }
}

-(void)getChangeRecord{
    [hud showAnimated:YES];
    [[ZPNetworking sharedZPNetworking] GET:APIUC@"class/getChangeRecord.json" parameters:@{@"id":[[[ZPAuthStore sharedZPAuthStore] userToDic] objectForKey:@"stuId"],@"pagesize":@"10",@"pagenumber":[NSString stringWithFormat:@"%li",(long)self.pagenumber]} progress:nil success:^(id  _Nonnull responseObject) {
        [hud hideAnimated:YES];
        if (CodeIs200(responseObject)) {
            if (_zhuanClassTableView.footer.isRefreshing) {
                [_zhuanClassTableView.footer endRefreshing];
            }
            _hasNextPage = [[[responseObject objectForKey:@"result"] objectForKey:@"hasNextPage"] boolValue];
            _pagenumber = [[[responseObject objectForKey:@"result"] objectForKey:@"pageNumber"] integerValue];
            NSArray * arr = [[responseObject objectForKey:@"result"] objectForKey:@"result"];
            for (NSDictionary *tmp in arr) {
                if ([toStr([tmp objectForKey:@"type"])isEqualToString:@"1"]) {
                    [_dataArr addObject:tmp];
                }
            }
            if (_dataArr.count==0) {
                _emptyView.hidden=NO;
                _zhuanClassTableView.hidden=YES;
            }else{
                _emptyView.hidden=YES;
                _zhuanClassTableView.hidden=NO;
            }
            if (!_hasNextPage) {
                [_zhuanClassTableView.footer noticeNoMoreData];
            }else{
                [_zhuanClassTableView.footer resetNoMoreData];
            }
            [_zhuanClassTableView reloadData];
            
        }else{
            [ZPPromptView showMessage:toStr([responseObject objectForKey:@"codeMsg"])];
            
        }
    } failure:^(NSError * _Nonnull error) {
        [hud hideAnimated:YES];
        if (_zhuanClassTableView.footer.isRefreshing) {
            [_zhuanClassTableView.footer endRefreshing];
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
